﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Data.SqlClient;

namespace BugReportingSystem
{
    public partial class Lapkhieunai : Form
    {
        SqlConnection conn;
        SqlDataAdapter da, da1,da2,da3,da4;
        SqlCommand cmd, cmd1,cmd2,cmd3,cmd4;
        SqlCommandBuilder cmb,cmb1;
        DataSet ds, ds1,ds2,ds3,ds4;
        DataTable dt, dt1,dt2,dt3,dt4;
        public Lapkhieunai()
        {
            InitializeComponent();
        }

        private void Lapkhieunai_Load(object sender, EventArgs e)
        {
            //Hien ds Hoa Don
            conn = new SqlConnection(ConfigurationSettings.AppSettings["conStr"]);
            cmd = new SqlCommand("select a.cID, d.kID, b.kTenkhachhang, a.sID, c.sTensanpham, d.hNgaymua, a.ctSoluong, c.sThoigianbaohanh, a.ctThanhtien from ChitietHoaDon a, Khachhang b,Sanpham c, Hoadon d where d.kID = b.kID and a.sid = c.sid and a.hID=d.hID;",conn);
            conn.Open();
            da = new SqlDataAdapter(cmd);
            ds = new DataSet();
            da.Fill(ds, "Hoa Don");
            dgv_HoaDon.DataSource = ds.Tables["Hoa Don"];
            conn.Close();

            //Hiển thị bảng ds đơn KN đã hoàn thành.
            cmd1 = new SqlCommand("select a.dID, b.kTenkhachhang, a.sID, c.sTensanpham, a.dLydokhieunai, a.dNgaylap, d.pbTenphongban, a.dNgayhentra, e.nvTendaydu, a.dTrangthai from Donkhieunai a, Khachhang b, Sanpham c, Phongban d, Nhanvien e where a.kID= b.kID and a.sID=c.sID and a.pbID=d.pbID and a.nvID = e.nvID;", conn);
            conn.Open();
            da1 = new SqlDataAdapter(cmd1);
            ds1 = new DataSet();
            da1.Fill(ds1, "Khieu nai");
            cmb1 = new SqlCommandBuilder(da1);
            dgv_Tab2.DataSource = ds1.Tables["Khieu nai"];
            conn.Close();

            //data đơn khiếu nại
            cmd3 = new SqlCommand("Select * from Donkhieunai", conn);
            conn.Open();
            da3 = new SqlDataAdapter(cmd3);
            ds3 = new DataSet();
            da3.Fill(ds3, "Bangkhieunai");
            cmb = new SqlCommandBuilder(da3);
            dt3 = ds3.Tables["Bangkhieunai"];
            conn.Close();

            //danh sách đơn KN chưa hoàn thành
            cmd4 = new SqlCommand("select a.dID, b.kTenkhachhang, a.sID, c.sTensanpham, a.dLydokhieunai, a.dNgaylap, d.pbTenphongban, a.dNgayhentra, a.dTrangthai from Donkhieunai a, Khachhang b, Sanpham c, Phongban d where a.kID= b.kID and a.sID=c.sID and a.pbID=d.pbID and a.nvID is null ;", conn);
            conn.Open();
            da4 = new SqlDataAdapter(cmd4);
             ds4 = new DataSet();
            da4.Fill(ds4, "Khieu nai");
            cmb1 = new SqlCommandBuilder(da4);
            dgvChuaTH.DataSource = ds4.Tables["Khieu nai"];
            conn.Close();

            cmd2 = new SqlCommand("SELECT * FROM Phongban", conn);
            da2 = new SqlDataAdapter(cmd2);
            ds2 = new DataSet();
            da2.Fill(ds2, "LoaiSP");
            dt2 = ds2.Tables["LoaiSP"];
            cbTenPhongban.DataSource = dt2;
            cbTenPhongban.DisplayMember = "pbTenphongban";
            cbTenPhongban.ValueMember = "pbID";

            cbPBSua.DataSource = dt2;
            cbPBSua.DisplayMember = "pbTenphongban";
            cbPBSua.ValueMember = "pbID";
            
        }

        private void Reload()
        {

            //danh sách đơn KN chưa hoàn thành
            conn.Open();
            da4 = new SqlDataAdapter(cmd4);
            ds4 = new DataSet();
            da4.Fill(ds4, "Khieu nai");
            cmb1 = new SqlCommandBuilder(da4);
            dgvChuaTH.DataSource = ds4.Tables["Khieu nai"];
            conn.Close();
        }

        private void btDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dgv_HoaDon_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            int dong = e.RowIndex;
            txtKid.Text = dgv_HoaDon.Rows[dong].Cells["kid"].Value.ToString();
            txtSid.Text = dgv_HoaDon.Rows[dong].Cells["sid"].Value.ToString();

            string tgBH = dgv_HoaDon.Rows[dong].Cells["sThoigianbaohanh"].Value.ToString();
           // string date = DateTime.Now.ToString("dd/MM/yyyy");
            //DateTime now = DateTime.Now;
            //DateTime tgBaoHanh = DateTime.Parse(tgBH);
            //TimeSpan sub = now.Subtract(tgBaoHanh);
            //double totaldate = sub.TotalDays;
            //MessageBox.Show(""+totaldate);

           // int check = ;
        }

        //public double tinhngay(){
        //    String duedatebook = duedate.Value.ToString();
        //    DateTime startdate = DateTime.Parse(duedatebook);
        //    DateTime now= DateTime.Now;
        //    TimeSpan ts = now.Subtract(startdate);
        //    double totaldate = ts.TotalDays;
        //    return totaldate;
        //}

        private void btOK_Click(object sender, EventArgs e)
        {
            try
            {
                String timkiem = cbTimkiem.SelectedItem.ToString();
               // MessageBox.Show(timkiem);
                if (timkiem=="Theo mã khách hàng")
                {
                    ds.Tables["Hoa Don"].DefaultView.RowFilter = "kID= " + int.Parse(txtTKiem.Text) + "";
                }
                else if (timkiem == "Theo tên khách hàng")
                {
                    ds.Tables["Hoa Don"].DefaultView.RowFilter = "kTenkhachhang LIKE '%" + txtTKiem.Text + "%'";
                }
            }
            catch
            {
                MessageBox.Show("Bạn chưa chọn kiểu tìm kiếm!");
            }
        }

        private void btHoanthanh_Click(object sender, EventArgs e)
        {
            dt3 = ds3.Tables["Bangkhieunai"];
            DataRow newRow = dt3.NewRow();
            newRow["kID"] = txtKid.Text;
            newRow["sID"] = txtSid.Text;
            newRow["dLydokhieunai"] = txtLydoKN.Text;
           // string date = DateTime.Now.ToString("dd/MM/yyyy");
            newRow["dNgaylap"] = DateTime.Today;
            newRow["pbID"] = cbTenPhongban.SelectedValue.ToString();
            dt3.Rows.Add(newRow);
            da3.Update(ds3, "Bangkhieunai");
            MessageBox.Show("Lập đơn thành công!");
            Reload();
        }

        private void btDongtab2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dgvChuaTH_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            int dong = e.RowIndex;
            txtKH.Text = dgvChuaTH.Rows[dong].Cells["Column2"].Value.ToString();
            txtSP.Text = dgvChuaTH.Rows[dong].Cells["Column4"].Value.ToString();
            txtNgaylap.Text = dgvChuaTH.Rows[dong].Cells["Column7"].Value.ToString();
            cbPBSua.Text = dgvChuaTH.Rows[dong].Cells["Column9"].Value.ToString();
            txtLydoSua.Text = dgvChuaTH.Rows[dong].Cells["Column5"].Value.ToString(); 
        }

        private void btHuybo_Click(object sender, EventArgs e)
        {
            txtSid.Text = "";
            txtKid.Text = "";
            txtLydoKN.Text = "";
            cbTenPhongban.Text = "(Chọn)";
        }

        private void btSua_Click(object sender, EventArgs e)
        {
            //dt3 = ds3.Tables["Bangkhieunai"];
            //int dong = dgvChuaTH.CurrentRow.Index;
            ////MessageBox.Show(""+dong);
            //String dID = dgvChuaTH.Rows[dong].Cells["Column1"].Value.ToString();
            //MessageBox.Show(dID);
            ////[] rows = dt3.Select("dID ="+dID+"");
            ////MessageBox.Show(""+rows);

            conn.Open();
           // dt3 = ds3.Tables["Bangkhieunai"];
            int dong = dgvChuaTH.CurrentRow.Index;
            String dID = dgvChuaTH.Rows[dong].Cells["Column1"].Value.ToString();
            SqlCommand cmd = new SqlCommand("UPDATE Donkhieunai set pbID = " + cbPBSua.SelectedValue + ",dLydokhieunai=N'" + txtLydoSua.Text + "' WHERE dID='"+dID+"'", conn);
            cmd.ExecuteNonQuery();
            MessageBox.Show("Sửa thông tin thành công!");
            conn.Close();
            Reload();
        }

        private void btRefresh_Click(object sender, EventArgs e)
        {
            txtKH.Text = "";
            txtSP.Text = "";
            txtLydoSua.Text = "";
            txtNgaylap.Text = "";
            cbPBSua.Text = "";
        }

        private void btDel_Click(object sender, EventArgs e)
        {
            conn.Open();
            dt3 = ds3.Tables["Bangkhieunai"];
            int dong = dgvChuaTH.CurrentRow.Index;
            String dID = dgvChuaTH.Rows[dong].Cells["Column1"].Value.ToString();
            DialogResult ketqua = MessageBox.Show("Bạn có chắc muốn xóa bản ghi này không?", "Thông báo", MessageBoxButtons.YesNo);
            if (ketqua == DialogResult.Yes)
            {
                SqlCommand cmd = new SqlCommand("DELETE Donkhieunai  WHERE dID='" + dID + "'", conn);
                cmd.ExecuteNonQuery();
                MessageBox.Show("Xóa thành công!");
                conn.Close();
                Reload();
            }

        }

        private void btOK1_Click(object sender, EventArgs e)
        {
            try
            {
                String timkiem = cbTK1.SelectedItem.ToString();
                // MessageBox.Show(timkiem);
                if (timkiem == "Theo mã khách hàng")
                {
                    ds1.Tables["Khieu nai"].DefaultView.RowFilter = "dID= " + int.Parse(txtTK1.Text) + "";
                }
                else if (timkiem == "Theo tên khách hàng")
                {
                    ds1.Tables["Khieu nai"].DefaultView.RowFilter = "TenKH LIKE '%" + txtTK1.Text + "%'";
                }
            }
            catch
            {
                MessageBox.Show("Bạn chưa chọn kiểu tìm kiếm!");
            }
        }

        private void btOK2_Click(object sender, EventArgs e)
        {
            try
            {
                String timkiem = cbTK2.SelectedItem.ToString();
                // MessageBox.Show(timkiem);
                if (timkiem == "Theo mã khách hàng")
                {
                    ds4.Tables["Khieu nai"].DefaultView.RowFilter = "Column1= " + int.Parse(txtTK2.Text) + "";
                }
                else if (timkiem == "Theo tên khách hàng")
                {
                    ds4.Tables["Khieu nai"].DefaultView.RowFilter = "Column2 LIKE '%" + txtTK2.Text + "%'";
                }
            }
            catch
            {
                MessageBox.Show("Bạn chưa chọn kiểu tìm kiếm!");
            }
        }


    }
}
